package com.bupt.smart.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bupt.smart.smart.R;
import com.bupt.smart.ui.activity.DeliveryActivity;
import com.bupt.smart.ui.activity.RepairActivity;
import com.bupt.smart.ui.adapter.HomeRecyclerViewAdapter;
import com.bupt.smart.ui.widget.DividerGridItemDecoration;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ght on 2016/3/23.
 */
public class HomeFragment extends Fragment{
    private static final int WUYEFEI_BTN = 0;
    private static final int WUYEBAOXIU_BTN = 1;
    private static final int SHEQUCHAOSHI_BTN = 2;
    private static final int SHEQULUNTAN_BTN = 3;
    private static final int FANGWUZULIN_BTN = 4;
    private static final int WODEKUAIDI_BTN = 5;

    private SliderLayout mImageSlider;
    private RecyclerView mRecyclerView;
    private HomeRecyclerViewAdapter mAdapter;
    private HashMap<String,String> imageUrls;
    private List<HomeRecyclerViewAdapter.ItemData> mData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.home_fragment, null);
        initView(v);
        initData();
        initListeners();
        return v;
    }

    @Override
    public void onStop() {
        super.onStop();
        mImageSlider.stopAutoCycle();
    }

    private void initView(View v) {
        mImageSlider = (SliderLayout) v.findViewById(R.id.img_slider);
        mRecyclerView = (RecyclerView) v.findViewById(R.id.home_rv);

        mImageSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
        mImageSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        mImageSlider.setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Visible);
        mImageSlider.setCustomAnimation(new DescriptionAnimation());
        mImageSlider.setDuration(3000);

        GridLayoutManager layoutManager = new GridLayoutManager(getContext(),4);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch (mAdapter.getItemViewType(position)) {
                    case HomeRecyclerViewAdapter.ITEM_VIEW_TYPE_FIRST_LINE:
                        return 2;
                    case HomeRecyclerViewAdapter.ITEM_VIEW_TYPE_OTHER_LINE:
                        return 1;
                    default:
                        return -1;
                }
            }
        });
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new DividerGridItemDecoration(getContext()));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    private void initData() {
        //设置轮播图url
        imageUrls = new HashMap<String, String>();
        imageUrls.put("Hannibal", "http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
        imageUrls.put("Big Bang Theory", "http://tvfiles.alphacoders.com/100/hdclearart-10.png");
        imageUrls.put("House of Cards", "http://cdn3.nflximg.net/images/3093/2043093.jpg");
        imageUrls.put("Game of Thrones", "http://images.boomsbeat.com/data/images/full/19640/game-of-thrones-season-4-jpg.jpg");

        for(String name : imageUrls.keySet()){
            TextSliderView textSliderView = new TextSliderView(getActivity());
            // initialize a SliderLayout
            textSliderView
                    .description(name)
                    .image(imageUrls.get(name))
                    .setScaleType(BaseSliderView.ScaleType.Fit);

            mImageSlider.addSlider(textSliderView);
        }

        //设置recyclerview
        mData = new ArrayList<HomeRecyclerViewAdapter.ItemData>();
        mData.add(new HomeRecyclerViewAdapter.ItemData(R.mipmap.btn_icon_wuyefei, getString(R.string.btn_wuyefei_txt)));
        mData.add(new HomeRecyclerViewAdapter.ItemData(R.mipmap.btn_icon_wuyebaoxiu, getString(R.string.btn_wuyebaoxiu_txt)));
        mData.add(new HomeRecyclerViewAdapter.ItemData(R.mipmap.btn_icon_shequchaoshi, getString(R.string.btn_shequchaoshi_txt)));
        mData.add(new HomeRecyclerViewAdapter.ItemData(R.mipmap.btn_icon_shequluntan, getString(R.string.btn_shequluntan_txt)));
        mData.add(new HomeRecyclerViewAdapter.ItemData(R.mipmap.btn_icon_fangwuzulin, getString(R.string.btn_fangwuzulin_txt)));
        mData.add(new HomeRecyclerViewAdapter.ItemData(R.mipmap.btn_icon_wodekuaidi, getString(R.string.btn_wodekuaidi_txt)));

        mAdapter = new HomeRecyclerViewAdapter(getContext(),mData);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initListeners() {
        mAdapter.setOnItemClickListener(new HomeRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = null;
                switch (position) {
                    case WUYEFEI_BTN:
                        break;
                    case WUYEBAOXIU_BTN:
                        intent = new Intent(getActivity(), RepairActivity.class);
                        getActivity().startActivity(intent);
                        break;
                    case SHEQUCHAOSHI_BTN:
                        break;
                    case SHEQULUNTAN_BTN:
                        break;
                    case FANGWUZULIN_BTN:
                        break;
                    case WODEKUAIDI_BTN:
                        intent = new Intent(getActivity(), DeliveryActivity.class);
                        getActivity().startActivity(intent);
                        break;
                }
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }
}
